﻿#include "cshowtreewidget.h"
#include "ccommomdata.h"

/*因为这是指定xml结构的解析，所以只有root节点和一级节点，现在暂时的拖动方法是将item拖动
到其可拖动的父对象上，就是只有拖动到root节点才会生效，而同一个root节点下想要交换，暂时是、
添加到最后，如果child1和child2想互换，只能把child1拖动到其root的节点，就会添加到child2后面，
这个TreeWidget拖动类参考了网上的一定量实现*/
CShowTreeWidget::CShowTreeWidget(QWidget *parent):
    QTreeWidget(parent)
{
    this->setDragEnabled(true);
    this->setAcceptDrops(true);
    this->setDragDropMode(QAbstractItemView::DragDropMode::InternalMove);

    m_plabel = new QLabel();
}

void CShowTreeWidget::mousePressEvent(QMouseEvent *event)
{
    m_pSelectItem = static_cast<QTreeWidgetItem*>(this->itemAt(event->pos()));
    //选中的是一级节点才能拖动
    if (event->button()==Qt::LeftButton && m_pSelectItem
            && m_pSelectItem->data(0, eRootRole).toString() != "root")
    {
        QByteArray dataItem;
        QDataStream dataStream(&dataItem, QIODevice::WriteOnly);        
        dataStream << m_pSelectItem->text(0) << m_pSelectItem->toolTip(0)
                   << m_pSelectItem->textColor(0);
        m_plabel->setText(m_pSelectItem->text(0));

        //设置自定义数据,用于区分是否可以拖动
        QMimeData* mimeData = new QMimeData;
        mimeData->setData("Data/dragNode", dataItem);

        //拖动设置
        QDrag* drag = new QDrag(this);
        drag->setPixmap(m_plabel->grab());
        drag->setMimeData(mimeData);
        drag->setHotSpot(QPoint(m_plabel->grab().width() / 2, m_plabel->grab().height() / 2));

        Qt::DropAction dropAction = drag->exec(Qt::MoveAction|Qt::CopyAction,Qt::CopyAction);
        if(dropAction == Qt::MoveAction)
        {
            //当成功拖动时清除原节点
            delete m_pSelectItem;
            m_pSelectItem = NULL;
        }
    }
    QTreeWidget::mousePressEvent(event);
}

void CShowTreeWidget::dragEnterEvent(QDragEnterEvent *event)
{
    //判断自定义类型
    if (event->mimeData()->hasFormat("Data/dragNode"))
    {
        if(children().contains(event->source()))
        {
           event->setDropAction(Qt::MoveAction);
           event->accept();
        }
        else
        {
           event->acceptProposedAction();
        }
    }
    else
    {
       event->ignore();
    }
}

void CShowTreeWidget::dragMoveEvent(QDragMoveEvent *event)
{
    //判断自定义类型
    if (event->mimeData()->hasFormat("Data/dragNode"))
    {
        if(children().contains(event->source()))
        {
           event->setDropAction(Qt::MoveAction);
           event->accept();
        }
        else
        {
           event->acceptProposedAction();
        }
    }
    else
    {
       event->ignore();
    }
}

void CShowTreeWidget::dropEvent(QDropEvent *event)
{
    if (event->mimeData()->hasFormat("Data/dragNode"))
    {
        //获取拖拽的数据
        QByteArray itemData = event->mimeData()->data("Data/dragNode");
        QDataStream dataStream(&itemData, QIODevice::ReadOnly);
        QString strText, strToolTip;
        QColor color;
        dataStream >> strText >> strToolTip >> color;
        QString redStr = QString("%1").arg(color.red(),2,16,QChar('0'));
        QString greenStr = QString("%1").arg(color.green(),2,16,QChar('0'));
        QString blueStr = QString("%1").arg(color.blue(),2,16,QChar('0'));
        QString strColor = "#"+ redStr + greenStr + blueStr;

        //获取放置的父节点,但是这边应该只让他在根节点下交换，不让在一级节点上交换
        QTreeWidgetItem *pItem = itemAt(event->pos());
        if (!pItem)
        {
            return;
        }
        else if(pItem == m_pSelectItem || pItem->data(0, eRootRole).toString() != "root")
        {
            return;
        }else
        {

        }

        //创建新节点
        QTreeWidgetItem* pNewItem = new QTreeWidgetItem(pItem);
        pNewItem->setText(0, strText);
        pNewItem->setToolTip(0, strToolTip);
        pNewItem->setTextColor(0, color);
        pNewItem->setData(0, eColorRole, strColor);
        pItem->setExpanded(true);

        if (event->source() == this)
        {
            event->setDropAction(Qt::MoveAction);
            event->accept();
        }
        else
        {
            event->acceptProposedAction();
        }
    }
    else
    {
        event->ignore();
    }
}
